﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using Microsoft.VisualBasic;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.DAO;
using BaseDAO = ERP_RORZE_TOOL.Tool.BCThoigianlamviecthang.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class ctrlMOStart : DevExpress.XtraEditors.XtraForm
    {
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable obj = new ClsMOTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();
        ClsOptionDataTable odt = new ClsOptionDataTable();
        ClsMODAO clsMODAO = new ClsMODAO();
        public event EventHandler Reload;

        private string DK1;
        private string DK2;
        private string DK3;
        private string DK4;
        private LogUtils log;
        private string DK5;

        public ctrlMOStart()
        {
            InitializeComponent();
            log = new LogUtils();

        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClsMOTable obj = ((ClsMOTable)cbMONo.SelectedItem);
            txtPartNo.Text = obj.PartID.ToString();
            txtQuantity.Text = obj.MOQty.ToString();
            //dateStartDate.Value = DateTime.Parse(obj.Startdate.ToString());
            checkStarted.Checked = bool.Parse(obj.Started.ToString());

            string i = txtPartNo.Text.Substring(txtPartNo.Text.Length - 2, 2);
            if (i == "+I")
            {
                DK1 = "UI";
                DK2 = "ZI";
                DK3 = "ZI";
                DK4 = "ZI";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+P")
            {
                DK1 = "UI";
                DK2 = "XO";
                DK3 = "YI";
                DK4 = "XH";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+R")
            {
                DK1 = "XU";
                DK2 = "ZI";
                DK3 = "ZI";
                DK4 = "ZI";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+B")
            {
                DK1 = "XB";
                DK2 = "XU";
                DK3 = "XV";
                DK4 = "ZI";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+H")
            {
                DK1 = "XL";
                DK2 = "XU";
                DK3 = "XV";
                DK4 = "ZI";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+A")
            {
                DK1 = "XH";
                DK2 = "UI";
                DK3 = "YI";
                DK4 = "XO";
                DK5 = "XO";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+F")
            {
                DK1 = "UI";
                DK2 = "YI";
                DK3 = "XU";
                DK4 = "ZI";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+K")
            {
                DK1 = "XL";
                DK2 = "XU";
                DK3 = "XV";
                DK4 = "XB";
                DK5 = "ZI";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else if (i == "+M")
            {
                DK1 = "XU";
                DK2 = "XU";
                DK3 = "XU";
                DK4 = "XU";
                DK5 = "XU";
                txtPartNo.Text = txtPartNo.Text.Substring(0, txtPartNo.Text.Length - 2);
            }
            else
            {
                DK1 = "AA";
                DK2 = "AA";
                DK3 = "AA";
                DK4 = "AA";
                DK5 = "AA";
                //txtPartNo.Text = txtPartNo.Text.Substring(0,txtPartNo.Text.Length - 2);
            }
        }

        private void ctrlMOStart_Load(object sender, EventArgs e)
        {
            dt = clsMODAO.GetAllMOStart();

            List<ClsMOTable> objs = new List<ClsMOTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsMOTable obj = new ClsMOTable();
                obj.MONo = r["MONo"].ToString();  //
                obj.PartID = r["PartID"].ToString();  //
                obj.MOQty = int.Parse(r["MOQty"].ToString());   ////
                //obj.Startdate = (DateTime)r["Startdate"];
                obj.Started = bool.Parse(r["Started"].ToString());     ////
                objs.Add(obj);
            }

            cbMONo.DataSource = objs;
            cbMONo.DisplayMember = "MONo";
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            //btnStart.Focus();
            DataTable dtMo = new DataTable();

            odt.PartID = txtPartNo.Text;
            dt = clsMODAO.GetOptionDataByMONo(odt);

            dtMo = BaseDAO.ExecuteQuery("SELECT * FROM [242_MO] WHERE MONo ='" + cbMONo.Text + "' and [Started]=1");

            //int i = 0;
            //foreach (DataRow r in dt.Rows)
            //{ 
            //    i++;
            //}
            
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Không có dữ liệu trong Optiondata. Hãy kiểm tra lại");
            }
            else if (dtMo.Rows.Count > 0)
            {
                MessageBox.Show("Đơn hàng đã bắt đầu rồi!");
            }
            else if (checkStarted.Checked == true)
            {
                MessageBox.Show("Đơn hàng này đã bắt đầu rồi. Hãy kiểm tra lại");
            }
            else
            {
                ClsMOMaterialTable MOM = new ClsMOMaterialTable();
                clsMODAO.InsertMOOptionAdd(cbMONo.Text, DK1, DK2, DK3, DK4, DK5);

                //write Log
                log.Table = LogConstant.Table.Product.MOOption;
                log.Task = LogConstant.Task.Insert;
                log.Description = "MONo: " + cbMONo.Text + "; DK1: " + DK1 + "; DK2: " + DK2 + "; DK3: " + DK3 + "; DK4: " + DK4 + "; DK5: " + DK5;
                log.WriteLog();
                //MessageBox.Show("Nhập dữ liệu vào MOOption xong");

                MOM.MONo = cbMONo.Text;
                clsMODAO.MOMaterialAppend(MOM);
                //write Log
                log.Table = LogConstant.Table.Product.MOMaterial;
                log.Task = LogConstant.Task.Insert;
                log.Description = "MONo: " + MOM.MONo;
                log.WriteLog();
                //MessageBox.Show("nhập dữ liệu vào MOMaterial xong");

                MO.MONo = cbMONo.Text;
                MO.Startdate = dateDate.Value;
                clsMODAO.UpDateMOStart(MO);
                //write Log
                log.Table = LogConstant.Table.Product.MOList;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + MO.MONo + "; Startdate: " + MO.Startdate;
                log.WriteLog();
                //MessageBox.Show("cập nhập dữ liệu vào MO xong");

                ClsBusOderTable bt = new ClsBusOderTable();
                bt.MONo = cbMONo.Text; 
                clsMODAO.UpDateBusoderMOStart(bt);

                //write Log
                log.Table = LogConstant.Table.Product.BusOrderList;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + bt.MONo;
                log.WriteLog();
                //MessageBox.Show("cập nhập dữ liệu vào Busoder xong");
                //Reload(sender, e);

                ctrlMOStart_Load(sender, e);
                txtPartNo.Text = "";
                txtQuantity.Text = "";
                cbMONo.Text = "";
                cbMONo.Focus();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void showmessage(object sender, EventArgs e)
        {
            MessageBox.Show("Đã cập nhật thành công");
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

    }
}
